package cn.tedu.boot22.controller;

import ch.qos.logback.core.db.dialect.DBUtil;
import cn.tedu.boot22.utils.DBUtils;
import cn.tedu.boot22.vo.Emp;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.*;
import java.util.ArrayList;

@RestController //作用:相当于在类上添加@Controller的同时还在每个方法上面添加了@ResponseBody
public class EmpController {

    @RequestMapping("/insert")
    public String insert(Emp emp){
        System.out.println("emp = " + emp);
        try (Connection conn = DBUtils.getConn()){
           /* String sql = "select id from myemp where name=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,emp.getName());
            ResultSet rs = ps.executeQuery();
            if (rs.next()){
                return "该员工已经存在";
            }*/
            String insertSql = "insert into myemp values(null,?,?,?)";
            PreparedStatement insertPs = conn.prepareStatement(insertSql);
            insertPs.setString(1,emp.getName());
            insertPs.setString(2,emp.getJob());
            insertPs.setInt(3,emp.getSal());
            insertPs.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "添加成功!<a href='/'>回到首页</a>";
    }

    @RequestMapping("/select")
    public String select(){
        ArrayList<Emp> list = new ArrayList<>();
        try(Connection conn = DBUtils.getConn()) {
            String sql = "select * from myemp";
            Statement s = conn.createStatement();
            ResultSet rs = s.executeQuery(sql);
            while(rs.next()){
                int id = rs.getInt(1);
                String name = rs.getString(2);
                String job = rs.getString(3);
                int sal = rs.getInt(4);
                //把查询到的数据添加到对象中 并添加到集合里面
                list.add(new Emp(id,name,job,sal));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String html="<table border=1>";
        html+="<caption>员工列表</caption>";
        html+="<tr><th>id</th><th>名字</th><th>工作</th><th>工资</th><th>操作<th></tr>";
        //添加多行员工信息
        for (Emp emp : list){
            html+="<tr>";
            html+="<td>"+emp.getId()+"</td>";
            html+="<td>"+emp.getName()+"</td>";
            html+="<td>"+emp.getJob()+"</td>";
            html+="<td>"+emp.getSal()+"</td>";
            html+="<td><a href='/delete?id="+emp.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html;
    }

    @RequestMapping("/update")
    public String update(Emp emp){
        System.out.println("emp = " + emp);
        try ( Connection conn = DBUtils.getConn()){
          String sql = "update myemp set name=?,job=?,sal=? where id=?";
          PreparedStatement ps = conn.prepareStatement(sql);
          ps.setString(1,emp.getName());
          ps.setString(2,emp.getJob());
          ps.setInt(3,emp.getSal());
          ps.setInt(4,emp.getId());
          ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "修改完成!<a href='/select'>返回列表页面</a>";
    }

    @RequestMapping("/delete")
    public String delete(int id){
        try(Connection conn = DBUtils.getConn()) {
            String sql = "delete from myemp where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            ps.executeUpdate(); //执行删除
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  "删除完成!<a href='/select'>返回列表页面</a>";
    }
}
